import { _decorator, Component, Node, Input, input, EventKeyboard, KeyCode, Vec3 } from 'cc';
import { MapGenerator } from './MapGenerator';

const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
    @property({ type: Number })
    moveSpeed: number = 200;

    @property({ type: MapGenerator })
    mapGenerator: MapGenerator | null = null;

    private targetPosition: Vec3 = new Vec3();
    private isMoving: boolean = false;

    onLoad() {
        // 初始化目标位置为当前位置
        this.targetPosition.set(this.node.position);

        // 设置键盘输入监听
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onDestroy() {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onKeyDown(event: EventKeyboard) {
        if (this.isMoving) return;

        const currentPos = this.node.position;
        let newX = currentPos.x;
        let newY = currentPos.y;

        switch (event.keyCode) {
            case KeyCode.KEY_W:
            case KeyCode.ARROW_UP:
                newY += 32;
                break;
            case KeyCode.KEY_S:
            case KeyCode.ARROW_DOWN:
                newY -= 32;
                break;
            case KeyCode.KEY_A:
            case KeyCode.ARROW_LEFT:
                newX -= 32;
                break;
            case KeyCode.KEY_D:
            case KeyCode.ARROW_RIGHT:
                newX += 32;
                break;
            default:
                return;
        }

        // 检查目标位置是否可行走
        if (this.canMoveTo(newX, newY)) {
            this.targetPosition.set(newX, newY, currentPos.z);
            this.isMoving = true;
        }
    }

    update(deltaTime: number) {
        if (this.isMoving) {
            const currentPos = this.node.position;
            const direction = new Vec3(
                this.targetPosition.x - currentPos.x,
                this.targetPosition.y - currentPos.y,
                0
            );

            // 归一化方向向量
            const distance = Vec3.len(direction);
            if (distance > 0) {
                Vec3.normalize(direction, direction);

                // 计算移动距离
                const moveDistance = this.moveSpeed * deltaTime;

                if (moveDistance >= distance) {
                    // 已到达目标位置
                    this.node.setPosition(this.targetPosition);
                    this.isMoving = false;
                } else {
                    // 向目标位置移动
                    this.node.setPosition(
                        currentPos.x + direction.x * moveDistance,
                        currentPos.y + direction.y * moveDistance,
                        currentPos.z
                    );
                }
            }
        }
    }

    // 检查目标位置是否可行走
    canMoveTo(x: number, y: number): boolean {
        if (!this.mapGenerator) return true;

        // 将像素坐标转换为地图瓦片坐标
        const tileX = Math.floor(x / 32);
        const tileY = Math.floor(y / 32);

        // 获取地形类型
        const tileType = this.mapGenerator.getTileAt(tileX, tileY);

        // 根据地形类型判断是否可行走
        switch (tileType) {
            case 'water':
            case 'mountain':
                return false;
            default:
                return true;
        }
    }
}